package smartfleet.mobile.rest.webservice.vehicle;

import smartfleet.mobile.rest.webservice.client.RestClient;
import smartfleet.mobile.util.parser.SmartFleetParser;
import smartfleet.mobile.vehicle.Vehicle;
import android.util.Log;

public class VehicleWebServiceUtil {

	static final String SERVICE_URL = "/smartfleetcserver/web/services/vehiclews";
	static final String START_UP_METHOD = "/startup";
	static final String REGISTER_METHOD = "/register";

	public static Vehicle getVehicleInfo(String idVehicle) {
		String url = SERVICE_URL + START_UP_METHOD + "/" + idVehicle;

		Vehicle vehicle = null;
		try {
			String response = RestClient.doGet(url);
			vehicle = SmartFleetParser.toVehicle(response);

		} catch (Exception e) {
			Log.d("Error: ", e.getMessage());
		}

		return vehicle;

	} //

	public static void registerOrUpdateVehicleInfo(Vehicle vehicle) {
		String url = SERVICE_URL + REGISTER_METHOD + "/";

		url += vehicle.getIdVehicle() + "/" + vehicle.getBatteryLevel() + "/"
				+ vehicle.getState() + "/" + vehicle.getSeatsAvailable()+ "/" +
				 vehicle.getCurrentAltitude()+"/"+vehicle.getCurrentLocation().getLatitude()+"/"+
				 vehicle.getCurrentLocation().getLongitude();
		try {
			String response = RestClient.doGet(url);
		    System.out.println("register vehicle info: "+response);
		} catch (Exception e) {
			System.out.println("Houver erro ao registar o veiculo no central server");
			e.printStackTrace();
 		}

	} //

	public static String updateVehicleState(int idVehicle, String state) {

		String url = "/update/state/";
		String response = "";

		try {

			response = RestClient.doGet(url + idVehicle + "/" + state);
			System.out.println(response);

		} catch (Exception e) {
			Log.d("Error: ", e.getMessage());
		}

		return response;
	} //

	public String updateBatteryLevel(int idVehicle, int batteryLevel) {

		String url = "/update/battery/level/";
		String response = "";

		try {

			response = RestClient.doGet(url + idVehicle + "/" + batteryLevel);
			System.out.println(response);

		} catch (Exception e) {
			Log.d("Error: ", e.getMessage());
		}

		return response;
	}

	public String updateVehicleCurrentLocation(int idVehicle,
			String currentLat, String currentLong) {

		String url = "/update/current/location/";
		String response = "";

		try {

			response = RestClient.doGet(url + idVehicle + "/" + currentLat
					+ "/" + currentLong);
			System.out.println(response);

		} catch (Exception e) {
			Log.d("Error: ", e.getMessage());
		}

		return response;

	}

	public String updateVehicleLastKnownLocation(int idVehicle,
			String idLocation) {

		String url = "/update/lastknown/location/";
		String response = "";

		try {

			response = RestClient.doGet(url + idVehicle + "/" + idLocation);
			System.out.println(response);

		} catch (Exception e) {
			Log.d("Error: ", e.getMessage());
		}

		return response;
	}

	public String updateVehicleNextDestination(int idVehicle, int idLocation) {

		String url = "/update/next/destination/";
		String response = "";

		try {

			response = RestClient.doGet(url + idVehicle + "/" + idLocation);
			System.out.println(response);

		} catch (Exception e) {
			Log.d("Error: ", e.getMessage());
		}

		return response;

	}

	public String updateVehicleAltitude(int idVehicle, double altitude) {

		String url = "/update/altitude/";
		String response = "";

		try {

			response = RestClient.doGet(url + idVehicle + "/" + altitude);
			System.out.println(response);

		} catch (Exception e) {
			Log.d("Error: ", e.getMessage());
		}

		return response;
	}
}
